package com.iamteer.hist.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iamteer.hist.dao.ProductDao;
import com.iamteer.hist.entity.Product;

/**
 * Servlet implementation class ProductDetail
 */
@WebServlet("/ProductDetail")
public class ProductDetail extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductDetail() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		
		ProductDao dao = new ProductDao();
		Product product = dao.findById(id);
		
		response.setContentType("text/html;charset=utf-8");
		PrintWriter writer = response.getWriter();
		String html = "";
		html += "<html>";
		html += "<head>";
		html += "<title>商品详情</title>";
		html += "</head>";
		html += "<body>";
		html += "<table border='1' align='center' width='600px'>";
		if (product != null) {
			html += "<tr><th>编号：</th><td>" + product.getId() + "</td></tr>";
			html += "<tr><th>商品名称：</th><td>" + product.getProName() + "</td></tr>";
			html += "<tr><th>商品型号：</th><td>" + product.getProType() + "</td></tr>";
			html += "<tr><th>商品价格：</th><td>" + product.getPrice() + "</td></tr>";
		} else {

		}

		html += "</table>";
		html += "<center><a href='" + request.getContextPath() + "/ProductList'>[返回列表]</a></center>";
		html += "</body>";
		html += "</html>";
		writer.write(html);
		
		/**
		 * 创建 Cookie 并发送
		 */

		Cookie cookie = new Cookie("prodHist", createId(request));
		cookie.setMaxAge(30*24*60*60);
		response.addCookie(cookie);
	}

	private String createId(HttpServletRequest request) {
		String id = request.getParameter("id");
		String prodHist = null;
		LinkedList<String> list;
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if ("prodHist".equals(cookie.getName())) {
					prodHist = cookie.getValue();
				}
			}
		}
		if (prodHist == null) {
			return id;
		} else {
			String[] ids = prodHist.split(",");
			List<String> colls = Arrays.asList(ids);
			list = new LinkedList<String>(colls);
//			if (list.size() < 3) {
//				if (list.contains(id)) {
//					list.remove(id);
//					list.addFirst(id);
//				} else {
//					list.addFirst(id);
//				}
//			} else {
//				if (list.contains(id)) {
//					list.remove(id);
//					list.addFirst(id);
//				} else {
//					list.removeLast();
//					list.addFirst(id);
//				}
//			}
			
//			if (list.size() < 3) {
//				if (list.contains(id)) {
//					list.remove(id);
//				}
//			} else {
//				if (list.contains(id)) {
//					list.remove(id);
//				} else {
//					list.removeLast();
//				}
//			}
//			list.addFirst(id);
			
			if (list.contains(id)) {
				list.remove(id);
			} else if (list.size() >= 3) {
				list.removeLast();
			}
			list.addFirst(id);
		}
		
		StringBuffer sb = new StringBuffer();
		for (Object o : list) {
			sb.append(o + ",");
		}
		String result = sb.toString();
		result = result.substring(0, result.length()-1);
		return result;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
